#include <stdio.h>
#include <math.h>

int random_int(int begin, int end) {
  if (begin <= end) {
    int divisor = end - begin + 1;
    int remainder = rand() % divisor;
    return remainder + begin;
  } else {
    return 0;
  }
}

double exponential(double mean) {
  double u = random_int(1, 1000000000) / 1000000000.0;
  return -mean * log(u);
}

double poisson(double mean) {
  double u = random_int(1, 1000000000) / 1000000000.0;
  double stop = exp(-mean);
  int k = 1;
  do {
    u *= random_int(1, 1000000000) / 1000000000.0;
    k++;
  } while (u > stop);

  return k - 1;
}
